<?php

class Index extends CI_Controller {

    function __construct() {
        parent::__construct();
        //$this->load->model("check_model", "check");
        $this->load->model("admin_menu_model");
        $this->load->model("car_model");
        $this->load->model("order_model");
        $this->load->model("order_fee_model");

        $this->load->service("admin_manage_service");
        $this->check = $this->admin_manage_service;
        $this->_adminInfo = $this->check->checkAdminLogin();

        $this->projectUrl = [
            "index" => MANAGE_URL . "&c=index",
            "main" => MANAGE_URL . "&c=index&m=main",
            "main2" => MANAGE_URL . "&c=index&m=main2",
            "refreshAskingOrder" => MANAGE_URL . "&c=order&m=refreshAskingOrder",
        ];

        $this->loginUrl = [
            "logout" => MANAGE_URL . "&c=login&m=logout",
        ];
        $this->adminUrl = [
            "editPassword" => MANAGE_URL . "&c=adminUser&m=editPassword",
        ];
    }

    /*
     * 首页
     */

    function index() {

        $data = [];

        $adminMenu = $this->admin_menu_model->getData("where status_id=1", 1, 0, "order by sort asc");
        $menu_data_tmp = [];
        foreach ($adminMenu as $k => $v) {
            $menu_data_tmp[$v["parent_id"]][$v["id"]] = $v;
        }
        $data["menu_data_tmp"] = $menu_data_tmp;

        $data["custom_name"] = $this->common->_base["web_kouhao"];

        $data["headTitle"] = "总后台管理-" . WEB_NAME;

        if ($this->_adminInfo["role"]["type_id"] == 2) {
            $mainUrl = $this->projectUrl["main2"];
        } else {
            $mainUrl = $this->projectUrl["main"];
        }
        $data["mainUrl"] = $mainUrl;

        $data["body_class"] = "layui-layout-body";
        $this->layout->view('admin/default/index', $data);
    }

    /*
     * 右边主体
     */

    public function main() {


        $orderNum = $this->order_model->getDataCount();
        $data["orderNum"] = $orderNum;

        $shouruFee = 0;
        $carFee = $this->order_fee_model->getData("where type_id=1", 1, 0, "", "sum(total_fee) as shouruFee");
        if (!empty($carFee)) {
            $shouruFee = $carFee[0]["shouruFee"];
        }
        $data["shouruFee"] = $shouruFee;

        $zhichuFee = 0;
        $carFee = $this->order_fee_model->getData("where type_id=2", 1, 0, "", "sum(total_fee) as zhichuFee");
        if (!empty($carFee)) {
            $zhichuFee = $carFee[0]["zhichuFee"];
        }
        $data["zhichuFee"] = $zhichuFee;

        $this->layout->view("admin/default/main", $data);
    }

    public function getWeekJobs() {

        $start_time = strtotime(date("Y-m-d", strtotime("-30 days")));
        $yundan = $this->order_fee_model->getData("where fee_date>=" . $start_time, 1, 0, "group by fee_date order by fee_date asc", "sum(total_fee) AS data_num,fee_date");
        //print_r($yundan);
        //$yundan = $this->_array_sort($yundan, 'fee_date', 'asc');
        //print_r($yundan);
        $xAxis_data = [];
        $series_data = [];
        foreach ($yundan as $k => $v) {
            $xAxis_data[] = date("m/d", $v["fee_date"]);
            $series_data[] = $v["data_num"];
        }

        $data = [
            "color" => ['#1E9FFF'],
            "title" => [
                "text" => ""
            ],
            "tooltip" => [
                "trigger" => 'axis',
            ],
            "legend" => [
                "data" => ['利润']
            ],
            "xAxis" => [
                "type" => 'category',
                "boundaryGap" => false,
                "data" => $xAxis_data,
            ],
            "yAxis" => (object) [],
            "grid" => [
                "left" => 55,
            ],
            "series" => [
                [
                    "name" => '利润',
                    "type" => 'line', //bar
                    //"stack" => 'Total',
                    "data" => $series_data,
                    //"data" => [5, 65442, 4543],
                    "markPoint" => [
                        //"symbol" => "pin",
                        "symbolSize" => "65",
                        //"symbolOffset" => [0, -1],
                        "label" => [
                            //"show" => true,
                            //"fontFamily" => "wgsFont",
                            "fontSize" => 11
                        ],
                        "data" => [
                            ["type" => 'max', "name" => "max"],
                            ["type" => 'min', "name" => "min"],
                        ]
                    ],
                    "markLine" => [
                        "data" => [
                            ["type" => 'average', "name" => "ave"],
                        ]
                    ],
                ]
            ],
        ];
        echo jsonEncode(["data" => $data]);
    }

    function _array_sort($array, $keys, $type = 'asc') {

        //$array为要排序的数组,$keys为要用来排序的键名,$type默认为升序排序

        $keysvalue = $new_array = array();

        foreach ($array as $k => $v) {

            $keysvalue[$k] = $v[$keys];
        }

        if ($type == 'asc') {

            asort($keysvalue);
        } else {

            arsort($keysvalue);
        }

        reset($keysvalue);

        foreach ($keysvalue as $k => $v) {

            $new_array[$k] = $array[$k];
        }

        return $new_array;
    }

    /*
     * 右边主体
     */

    public function main2() {


        $data = [];

        $this->layout->view("admin/default/main2", $data);
    }

}
